﻿// <copyright file="HeaderMetadataSection.cs" company="Sinclair Community College">
// Copyright 2010 Sinclair Community College
// </copyright>
// <originalAuthor>Patrick S. Seymour (patrick.seymour@gmail.com)</originalAuthor>

namespace SinclairCC.AppV
{
    using System;

    /// <summary>
    /// This class forms a base class for the metadata header sections that every
    /// SFT file contains.
    /// </summary>
    public abstract class HeaderMetadataSection
    {
        /// <summary>
        /// The position (byte) in the SFT file at which the header section starts.
        /// </summary>
        /// <remarks>
        /// This field is not mentioned in the specification. It is merely recorded
        /// for developer convenience.
        /// </remarks>
        private long startPositionInFile;

        /// <summary>
        /// The section header data common to all headers.
        /// </summary>
        private CommonSectionHeader commonHeader;

        /// <summary>
        /// Indicates whether the entire section has been read from the file.
        /// </summary>
        private bool sectionHasBeenRead;

        /// <summary>
        /// A binary file reader that points to the SFT file.
        /// </summary>
        private System.IO.BinaryReader fileReader;

        /// <summary>
        /// Initializes a new instance of the HeaderMetadataSection class.
        /// </summary>
        /// <param name="fileReader">
        /// A binary file reader that points to the SFT file.
        /// </param>
        protected HeaderMetadataSection(System.IO.BinaryReader fileReader)
        {
            this.fileReader = fileReader;
            this.sectionHasBeenRead = false;

            this.startPositionInFile = fileReader.BaseStream.Position;

            this.commonHeader = null;
            if ((fileReader.BaseStream.Length - (this.startPositionInFile + 1)) >= 9)
            {
                this.commonHeader = new CommonSectionHeader(fileReader.ReadBytes(9));
            }

            fileReader.BaseStream.Seek(this.startPositionInFile + this.commonHeader.SectionSize, System.IO.SeekOrigin.Begin);
        }

        /// <summary>
        /// Gets the position (byte) in the SFT file at which the header section starts.
        /// </summary>
        protected long StartPositionInFile
        {
            get { return this.startPositionInFile; }
        }

        /// <summary>
        /// Gets the common section header that each section contains.
        /// </summary>
        protected CommonSectionHeader CommonHeader
        {
            get { return this.commonHeader; }
        }

        /// <summary>
        /// Gets the BinaryReader that is being used to read the file.
        /// </summary>
        protected System.IO.BinaryReader FileReader
        {
            get { return this.fileReader; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the entire section has been read.
        /// </summary>
        protected bool SectionHasBeenRead
        {
            get { return this.sectionHasBeenRead; }
            set { this.sectionHasBeenRead = value; }
        }

        /// <summary>
        /// Reads the data for the entire section.
        /// </summary>
        protected abstract void ReadSection();
    }
}
